package com.adams.sys.menu.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.adams.access.db.bean.Menu;
import com.adams.access.db.dao.hibernate.HibernateDao;
import com.adams.sys.menu.dao.MenuDao;
import com.adams.utils.Page;

/**
 * 
 * 
 * File Name : MenuDaoImpl.java
 * 
 * @Description : 菜单数据操作类,提供菜单的增、删、改和查功能
 * @author 902990封文进
 */
@Repository("menuDao")
public class MenuDaoImpl extends HibernateDao<Menu, Long> implements MenuDao
{

	/**
	 * 
	 * Description : 根据分页查询所有菜单
	 * 
	 * @param page
	 * @return Page<Menu>
	 * 
	 */
	public Page<Menu> queryAllByPage(Page<Menu> page)
	{
		return queryPage(page, "from Menu as m where m.visible=1 order by m.ordinal");
	}

	/**
	 * 
	 * Description : 根据菜单码查询菜单
	 * 
	 * @param code
	 * @return Menu
	 * 
	 */
	public Menu queryByCode(String code)
	{
		List<Menu> list = query("from Menu as m where m.menuCode=?", code);
		if (list != null && list.size() > 0)
		{
			return list.get(0);
		}
		return null;
	}

	/**
	 * 
	 * Description : 根据父菜单编号查询子菜单列表
	 * 
	 * @param parentId
	 * @return List<Menu>
	 * 
	 */
	public List<Menu> querySubMenus(Long parentId)
	{
		return query(
				"from Menu as m where m.parentId=? and m.visible=1 order by ordinal",
				parentId);
	}

	/**
	 * Description : 查询所有的子菜单列表
	 * 
	 * @return
	 * 
	 */
	public List<Menu> queryAllSubMenus()
	{
		return query("from Menu as m where m.parentId>0 order by ordinal");
	}

	/**
	 * 
	 * Description : 获取所有菜单
	 * 
	 * @return
	 * 
	 */
	public List<Menu> queryAll()
	{
		return query("from Menu as m where m.visible=1 order by ordinal");
	}

	/**
	 * Description:获取最大排序值
	 * @return
	 */
	public Integer queryMaxOrdinal() {
		List result = (List)createQuery("select max(ordinal) from Menu").list();
		if(result.size() >= 0){
			return (Integer)result.get(0);
		}
		return null;
	}

}
